{% extends "h:templates/layouts/admin.html.jinja2" %}

{% from '../includes/search_form.html.jinja2' import search_form %}

{% set page_id = 'groups' %}
{% set page_title = 'Groups' %}

{% block content %}
  <p>
    On this page you can see a list of all the groups and their details.
  </p>

  {{ search_form('Search by name…', request.params.get('q')) }}

  <div class="table-responsive">
    <table class="table table-striped">
      <thead>
        <tr>
          <th>Name</th>
          <th>URL</th>
          <th>Organization</th>
          <th>Created by</th>
          <th>Members</th>
        </tr>
      </thead>
      <tbody>
        {% for group in results %}
          <tr>
            <td>
              {% set edit_url = request.route_url('admin.groups_edit', id=group.pubid) %}
              <a href="{{ edit_url }}">{{ group.name }}</a>
            </td>
            <td>
              {% set group_url = request.route_url('group_read', pubid=group.pubid, slug=group.slug) %}
              <a href="{{ group_url }}">
                {{ group_url }}
              </a>
            </td>
            <td>
            {% if group.organization %}
              {% set org_url = request.route_url('admin.organizations_edit', pubid=group.organization.pubid) %}
              <a href="{{ org_url }}">{{ group.organization.name }}</a>
            {% endif %}
            </td>
            <td>
              {% if group.creator %}
                <a href="{{ request.route_url('admin.users', _query={'username': group.creator.username, 'authority': group.creator.authority}) }}">
                  {{ group.creator.username }}
                </a>
              {% endif %}
            </td>
            <td>{{ group.members|length }}</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>

  {% include "h:templates/includes/paginator.html.jinja2" %}
{% endblock %}
